package cn.echisan.homework.service.impl;

import cn.echisan.homework.dao.UserMapper;
import cn.echisan.homework.enums.Role;
import cn.echisan.homework.model.User;
import cn.echisan.homework.model.UserExample;
import cn.echisan.homework.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by echisan on 2018/5/29
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public User getUserByIdentifyNum(String identifyNum) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andIdentifyNumEqualTo(identifyNum);
        List<User> users = userMapper.selectByExample(userExample);
        if (users == null || users.size() == 0){
            return null;
        }
        return users.get(0);
    }

    @Override
    public List<User> listStudents() {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andRoleEqualTo(Role.ROLE_STUDENT.name());
        return userMapper.selectByExample(userExample);
    }

    @Override
    public List<User> listTeachers() {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andRoleEqualTo(Role.ROLE_TEACHER.name());
        return userMapper.selectByExample(userExample);
    }

    @Override
    public Integer insertUser(User user) {
        return userMapper.insertSelective(user);
    }

    @Override
    public Integer updateUser(User user) {
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public Integer deleteUserById(Integer id) {
        return userMapper.deleteByPrimaryKey(id);
    }
}
